//
//
//package cn.shengchao.event.distribute;
//
//import cn.hutool.core.collection.CollUtil;
//import cn.shengchao.base.enums.ProductType;
//import cn.shengchao.examstar.distribute.event.OrderTaskCreateEvent;
//import cn.shengchao.examstar.distribute.infrastrure.po.DistributeOrderTask;
//import cn.shengchao.examstar.distribute.infrastrure.service.DistributeOrderTaskLogService;
//import cn.shengchao.examstar.distribute.infrastrure.service.DistributeOrderTaskService;
//import cn.shengchao.examstar.distribute.ohs.local.appservice.DistributeOrderDomainService;
//import cn.shengchao.examstar.rpc.distribute.dto.DistributeOrderContentDto;
//import cn.shengchao.examstar.pay.infrastructure.dao.PayOrderItemDao;
//import cn.shengchao.examstar.pay.infrastructure.dao.po.PayOrderItem;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.event.EventListener;
//import org.springframework.scheduling.annotation.Async;
//import org.springframework.stereotype.Component;
//
//import java.util.List;
//
///**
// * @program: examstarapi
// * @description: 分销业务事件监听
// * @author: Xiong weiqiang
// * @create: 2025-03-28 14:26
// */
//@Component
//public class DistributeBusinessListener {
//    @Autowired
//    private DistributeOrderDomainService distributeOrderDomainService;
//    @Autowired
//    private DistributeOrderTaskLogService distributeOrderTaskLogService;
//    @Autowired
//    private DistributeOrderTaskService distributeOrderTaskService;
//    @Autowired
//    private PayOrderItemDao payOrderItemDao;
//
//    /**
//     * 订单创建任务监听
//     *
//     * @param orderTaskCreateEvent
//     */
//    @Async
//    @EventListener(classes = OrderTaskCreateEvent.class)
//    public void orderDistribute(OrderTaskCreateEvent orderTaskCreateEvent) {
//        Long orderId = orderTaskCreateEvent.getOrderId();
//        Long taskId = orderTaskCreateEvent.getTaskId();
//        DistributeOrderTask distributeOrderTask = distributeOrderTaskService.getById(taskId);
//        if (distributeOrderTask==null){
//            try {
//                Thread.sleep(3000L);
//                distributeOrderTask = distributeOrderTaskService.getById(taskId);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        }
//        if (distributeOrderTask==null){
//            distributeOrderTaskLogService.logError(taskId, orderId, "订单创建任务监听，订单商品获取为空");
//            return;
//        }
//        if (ProductType.PRODUCT_MEMBER.name().equals(distributeOrderTask.getProductType())){
//            distributeOrderDomainService.processMemberDistributeOrderTask(taskId, orderId);
//        }else if (ProductType.COURSE.name().equals(distributeOrderTask.getProductType())||ProductType.COURSE_NODES.name().equals(distributeOrderTask.getProductType())){
//            List<PayOrderItem> payOrderItems = payOrderItemDao.selectList(Wrappers.lambdaQuery(PayOrderItem.class)
//                    .eq(PayOrderItem::getOrderId, orderId));
//            if (CollUtil.isNotEmpty(payOrderItems)) {
//                List<DistributeOrderContentDto> orderContentDtoList = payOrderItems.stream()
//                        .map(a -> DistributeOrderContentDto.builder()
//                                .productId(a.getProductId())
//                                .productName(a.getProductName())
//                                .productAmount(a.getTotalAmount())
//                                .orderId(a.getOrderId())
//                                .build())
//                        .toList();
//                distributeOrderDomainService.processContentDistributeOrderTask(taskId, orderId, orderContentDtoList);
//            } else {
//                distributeOrderTaskLogService.logError(taskId, orderId, "订单创建任务监听，订单商品为空");
//            }
//        }else {
//            List<PayOrderItem> payOrderItems = payOrderItemDao.selectList(Wrappers.lambdaQuery(PayOrderItem.class)
//                    .eq(PayOrderItem::getOrderId, orderId));
//            if (CollUtil.isNotEmpty(payOrderItems)) {
//                List<DistributeOrderContentDto> orderContentDtoList = payOrderItems.stream()
//                        .map(a -> DistributeOrderContentDto.builder()
//                                .productId(a.getProductId())
//                                .productName(a.getProductName())
//                                .productAmount(a.getTotalAmount())
//                                .orderId(a.getOrderId())
//                                .build())
//                        .toList();
//                distributeOrderDomainService.processContentDistributeOrderTask(taskId, orderId, orderContentDtoList);
//            } else {
//                distributeOrderTaskLogService.logError(taskId, orderId, "订单创建任务监听，订单商品为空");
//            }
//        }
//
//
//    }
//}